package com.tuju.jetpackfirstdemo.utils;

import android.content.Context;
import android.content.res.Resources;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileLockInterruptionException;

/**
 * Created by wtm on 2017/11/17.
 */
public class TextResourceReader {

    private TextResourceReader() {
    }

    /**
     * 从raw中读取String
     *
     * @param context
     * @param resourceId
     * @return
     */
    public static String readTextFileFromRawResource(Context context, int resourceId) {
        StringBuilder sb = new StringBuilder();
        try {
            InputStream inputStream = context.getResources().openRawResource(resourceId);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String nextLine = null;
            while ((nextLine = bufferedReader.readLine()) != null) {
                sb.append(nextLine);
                sb.append('\n');
            }
        } catch (IOException e) {
            throw new RuntimeException("Could not open resource:" + resourceId, e);
        } catch (Resources.NotFoundException e) {
            throw new RuntimeException("Resource not find:" + resourceId, e);
        }
        return sb.toString();
    }

    /**
     * 从assets中读取String
     *
     * @param context
     * @param path
     * @return
     */
    public static String readTextFileFromAssets(Context context, String path) {
        InputStream is = null;
        try {
            is = context.getResources().getAssets().open(path);
        } catch (IOException e) {
            throw new RuntimeException(TextResourceReader.class.getName()
                    + ".readFileFromAssets---->" + path + " not found");
        }
        return inputStream2String(is);
    }

    /**
     * 从assets中读取String
     */
    public static String readTextFileFromAssetsCanNull(Context context, String path) {
        InputStream is = null;
        try {
            is = context.getResources().getAssets().open(path);
        } catch (IOException e) {
            return null;
        }
        return inputStream2String(is);
    }

    /**
     * 从SDCard中读取String
     *
     * @param path
     * @return
     * @throws FileNotFoundException
     * @throws FileLockInterruptionException
     */
    public static String readTextFileFromSDCard(String path)
            throws FileNotFoundException, FileLockInterruptionException {
        InputStream is = null;
        File file = new File(path);
        if (!file.exists()) {
            throw new FileNotFoundException(TextResourceReader.class.getName()
                    + ".readFileFromSDCard---->" + path + " not found");
        }
        if (!file.canRead()) {
            throw new FileLockInterruptionException();
        }
        return inputStream2String(new FileInputStream(file));
    }
    public static String readTextFileFromSDCardText(String path) {
        InputStream is = null;
        String result = "";
        File file = new File(path);
        if (!file.exists()) {
            return result;
        }
        if (!file.canRead()) {
            return result;
        }
        try {
            result = inputStream2String(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            result = "";
        }
        return result;
    }

    private static String inputStream2String(InputStream is) {
        if (null == is) {
            return null;
        }
        StringBuilder resultSb = null;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            resultSb = new StringBuilder();
            String len;
            while (null != (len = br.readLine())) {
                resultSb.append(len);
            }
        } catch (Exception ex) {
        } finally {
            IOUtils.closeIO(is);
        }
        return null == resultSb ? null : resultSb.toString();
    }

}
